<?php

namespace app\manage\controller;

use think\Db;
use app\manage\model\Menu as MenuModel;

class Menu extends Common
{
    public function index($tab = '1', $id = 0)
    {
        $menuModel = new MenuModel();
            $menu = $menuModel->select();
            $this->assign('menu', $menu);
            //编辑菜单
            if (3 == $tab) {
                $menu_info = Db::name('menu')->where('id', $id)->find();
                if ($menu_info != null && is_array($menu_info)) {
                    $this->assign('menu_info', $menu_info);
                }
            }
        return view();
    }

    //新增
    public function add()
    {
        if (request()->isPost()) {
            $data = input('post.');
            $menuModel = new MenuModel();
            if ($menuModel->save($data)) {
                return success('添加成功', url('index', ['tab' => 1]));
            } else {
                return error('添加失败');
            }
        }
    }

    //修改信息
    public function edit()
    {
        if (request()->isPost()) {
            $data = input('post.');
            $menuModel = new MenuModel();
            if ($menuModel->allowField(true)->isUpdate()->save($data)) {
                return success('编辑成功!', url('index', ['tab' => 1]));
            } else {
                return error('编辑失败或者是未修改!');
            }
        }
    }
    //删除
    public function delete($id){
        $menuModel = new MenuModel();
        $menu = $menuModel::destroy($id);
        if ($menu) {
            return success('删除成功！', url('index', ['tab' => 1]));
        } else {
            return error('删除失败！');
        }
    }
}
